<?php
namespace app\admin\model;

class Valuation extends Base{

    protected $name = 'valuation';
    const STOCK_BASE_INFO = 'https://emweb.eastmoney.com/PC_HSF10/ProfitForecast/PageAjax?code=';

    //盈利预测
    public function getStockValuation($val=[], $sid=0){

        $val['stock_code'] = trim($val['stock_code']);
        $reVal = '';
        $reVal = $this->getUrl(self::STOCK_BASE_INFO.$val['jys'].$val['stock_code']);
        if(!empty($reVal)){
            $reVal = json_decode($reVal,true);
            $tmpData = ['stock_code'=>$val['stock_code'],'content'=>'','year'=>$reVal['yctj_chart'][2]['YEAR']];
            $tmpData['val_type'] = 'mgsy';
            $tmpData['value'] = sprintf("%01.2f", $reVal['yctj_chart'][2]['EPS']);
            $tmpData['ratio'] = sprintf("%01.2f", $reVal['yctj_chart'][2]['EPS_RATIO']);
            $this->valuationSave($tmpData, $sid);
        }
    }

    protected function valuationSave($data=[],$sid){

        $tmp = [];
        $tmp['val_type'] = $data['val_type'];
        $tmp['year'] = intval($data['year']);
        $tmp['value'] = $data['value'];
        $tmp['ratio'] = $data['ratio'];
        $tmp['content'] = '';
        $valuation = self::where(['sid'=>$sid])->find();
        if(!empty($valuation['vid'])){
            $this->where(['vid'=>$valuation['vid']])->update($tmp);
        }else{
            $tmp['sid'] = $sid;
            $tmp['stock_code'] = $data['stock_code'];
            $this->insert($tmp);
        }
    }

    //删除
    public function delStockValuation($sid=0, $stock_code=''){
        $this->where(['sid'=>$sid,'stock_code'=>$stock_code])->delete();
    }

    //详情-经营分析
    public function detailValuation($sid=0, $stock_code=''){

        return $this->where(['sid'=>$sid,'stock_code'=>$stock_code])->order('vid desc')->select();
    }
}